package com.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.bean.Student;
import com.study.mapper.StudentMapper;
import com.study.service.StudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author mengxj
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentMapper studentMapper;

    @Override
    public List<String> studentNames() {
        return this.studentMapper.studentNames();
    }

    //原生的，自己写sql的方法
    @Override
    public List<Student> students1(Integer pageNum, Integer pageSize) {
        pageNum = (pageNum - 1) * pageSize;
        return this.studentMapper.selectStudent(Arrays.asList("李四", "王三", "张星星"), 1);
    }

    //利用pagehelper实现分页
    @Override
    public List<Student> students2(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return this.studentMapper.selectAll();
    }

    @Override
    public PageInfo<Student> students2_1(Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<Student> students = this.studentMapper.selectAll();
        return new PageInfo<>(students);
    }

    //mybatis-plus   推荐
    @Override
    public List<Student> students3(Integer pageNum, Integer pageSize) {
        Page<Student> page = new Page<>(pageNum, pageSize);
        return this.studentMapper.selectMybatisPlus(page, new QueryWrapper<>());
    }

    @Override
    public IPage<Student> students31(Integer pageNum, Integer pageSize) {
        Page<Student> page = new Page<>(pageNum, pageSize);
        return this.studentMapper.selectMybatisPlus1(page, new QueryWrapper<>());
    }

    @Override
    public List<Student> studentByParam(Integer age, String address, String orderByColumn) {
        return this.studentMapper.studentList(age, address, orderByColumn);
    }

    @Override
    public List<Student> studentByParamPlus(Integer age, String address, String orderByColumn) {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        // 1 = 1 and age > ? and address like '%where%' order by ? asc
        wrapper.le("age", age)
                .like("address", address);
        wrapper.orderByAsc(orderByColumn);
        return this.studentMapper.selectList(wrapper);
    }

    private List<Student> studentByParamPlus1(Integer age, String address, String orderByColumn) {
        LambdaQueryWrapper<Student> wrapper = new QueryWrapper<Student>().lambda();
        wrapper.le(Student::getAge, age)
                .like(Student::getAddress, address);
        wrapper.last(String.format(" order by %s asc", orderByColumn));
        return this.studentMapper.selectList(wrapper);
    }

    @Override
    public IPage<Student> studentByParamPlusPage(Integer age, String address, String orderByColumn) {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.like("address", address);
        wrapper.orderByAsc(orderByColumn);
        Page<Student> page = new Page<>(1, 3);
        return this.studentMapper.selectPage(page, wrapper);
    }

    @Override
    public String insert() {
        Student student = new Student();
        student.setAge(43);
        student.setAddress("北京");
        student.setSchoolId(3L);
        student.setId(11L);
        student.setName("aaa");
        System.out.println(student.getId());
        this.studentMapper.updateById(student);
        System.out.println(student.getId());
        return "success";
    }
}
